home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / netuser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-25  |  3.8 KB  |  195 lines

  1. /* Miscellaneous interger and IP address format conversion subroutines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #define LINELEN 256
  5. #include <ctype.h>
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "netuser.h"
  9. #include "domain.h"
  10.  
  11. int Net_error;
  12.  
  13. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  14.  * binary IP address
  15.  */
  16. int32
  17. aton(s)
  18. register char *s;
  19. {
  20.     int32 n;
  21.  
  22.     register int i;
  23.  
  24.     n = 0;
  25.     if(s == NULLCHAR)
  26.         return 0;
  27.     for(i=24;i>=0;i -= 8){
  28.         /* Skip any leading stuff (e.g., spaces, '[') */
  29.         while(*s != '\0' && !isdigit(*s))
  30.             s++;
  31.         if(*s == '\0')
  32.             break;
  33.         n |= (int32)atoi(s) << i;
  34.         if((s = strchr(s,'.')) == NULLCHAR)
  35.             break;
  36.         s++;
  37.     }
  38.     return n;
  39. }
  40.  
  41. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  42.  * string, e.g., 255.255.255.255\0
  43.  */
  44. char *
  45. inet_ntoa(a)
  46. int32 a;
  47. {
  48.     static char buf[25];
  49.     char *name;
  50.  
  51.     if(DTranslate && (name = resolve_a(a,!DVerbose)) != NULLCHAR) {
  52.         strncpy(buf, name, 24);
  53.         buf[24] = '\0';
  54.         free(name);
  55.     } else {
  56.         sprintf(buf,"%u.%u.%u.%u",
  57.             hibyte(hiword(a)),
  58.             lobyte(hiword(a)),
  59.             hibyte(loword(a)),
  60.             lobyte(loword(a)) );
  61.     }
  62.     return buf;
  63. }
  64.  
  65. /* Convert an internet address (in host byte order) to a unformated string
  66.  */
  67. char *
  68. inet_ntobos(a)
  69. int32 a;
  70. {
  71.     static char buf[5];
  72.  
  73.         buf[0]=hibyte(hiword(a));
  74.         buf[1]=lobyte(hiword(a));
  75.         buf[2]=hibyte(loword(a));
  76.         buf[3]=lobyte(loword(a));
  77.         buf[4]='\0';
  78.  
  79.         return buf;
  80. }
  81.  
  82. /* Convert hex-ascii string to long integer */
  83. long
  84. htol(s)
  85. char *s;
  86. {
  87.     long ret;
  88.     char c;
  89.  
  90.     ret = 0;
  91.     while((c = *s++) != '\0'){
  92.         c &= 0x7f;
  93.         if(c == 'x')
  94.             continue;    /* Ignore 'x', e.g., '0x' prefixes */
  95.         if(c >= '0' && c <= '9')
  96.             ret = ret*16 + (c - '0');
  97.         else if(c >= 'a' && c <= 'f')
  98.             ret = ret*16 + (10 + c - 'a');
  99.         else if(c >= 'A' && c <= 'F')
  100.             ret = ret*16 + (10 + c - 'A');
  101.         else
  102.             break;
  103.     }
  104.     return ret;
  105. }
  106. char *
  107. pinet(s)
  108. struct socket *s;
  109. {
  110.     static char buf[80];
  111.     char port[11];
  112.  
  113.     switch(s->port) {
  114.         case 7:            /* Echo data port */
  115.             sprintf(port,"echo");
  116.             break;
  117.         case 9:            /* Discard data port */
  118.             sprintf(port,"discard");
  119.             break;
  120.         case 13:        /* Daytime data port */
  121.             sprintf(port,"daytime");
  122.             break;
  123.         case 17:        /* Quote of the day data port */
  124.             sprintf(port,"quote");
  125.             break;
  126.         case 20:        /* FTP Data port */
  127.             sprintf(port,"ftpd");
  128.             break;
  129.         case 21:        /* FTP Control port */
  130.             sprintf(port,"ftp");
  131.             break;
  132.         case 23:        /* Telnet port */
  133.             sprintf(port,"telnet");
  134.             break;
  135.         case 25:        /* Mail port */
  136.             sprintf(port,"smtp");
  137.             break;
  138.         case 37:        /* Time data port */
  139.             sprintf(port,"time");
  140.             break;
  141.         case 53:        /* Domain Nameserver */
  142.             sprintf(port,"domain");
  143.             break;
  144.         case 69:        /* TFTP Data port */
  145.             sprintf(port,"tftpd");
  146.             break;
  147.         case 79:        /* Finger port */
  148.             sprintf(port,"finger");
  149.             break;
  150.         case 87:        /* Ttylink port */
  151.             sprintf(port,"ttylink");
  152.             break;
  153.         case 109:       /* POP2 port */
  154.             sprintf(port,"pop2");
  155.             break;
  156.         case 110:       /* POP3 port */
  157.             sprintf(port,"pop3");
  158.             break;
  159.         case 119:        /* NNTP port */
  160.             sprintf(port,"nntp");
  161.             break;
  162.         case 513:        /* RLOGIN port */
  163.             sprintf(port,"rlogin");
  164.             break;
  165.         case 520:        /* Routing Information Protocol */
  166.             sprintf(port,"rip");
  167.             break;
  168.         case 1234:        /* Pulled out of the air */
  169.             sprintf(port,"remote");
  170.             break;
  171.         case 1235:
  172.             sprintf(port,"callbook");
  173.             break;
  174.         case 3597:
  175.             sprintf(port,"news");
  176.             break;
  177.         case 3598:
  178.             sprintf(port,"info");
  179.             break;
  180.         case 3599:
  181.             sprintf(port,"tutor");
  182.             break;
  183.         case 3600:
  184.             sprintf(port,"conference");
  185.             break;
  186.         default:
  187.             sprintf(port,"%u",s->port);
  188.             break;
  189.     }
  190.  
  191.     sprintf(buf,"%s:%s",inet_ntoa(s->address),port);
  192.     return buf;
  193. }
  194.  
  195.